package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 封装Response对象，需要依赖于OutputStream
 *
 * 该对象需要提供核心方法，输出html
 */
public class Response {

    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }


    // 使用输出流输出指定字符串
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
        outputStream.flush();
    }


    /**
     *  输出静态html页面  url--->/index.html
     */
    public void outputHtml(String path,Mapper mapper) throws IOException {
        String absoluteResourcePath = "";
        String context = path.substring(1, path.indexOf("/", 1));
        for(Context Context : mapper.getHosts().get(0).getContexts()){
            if(context.equals(Context.getApplicationName())){
                absoluteResourcePath = Context.getApplicationPath() + path.substring(context.length()+1) ;
            }
        }
        // 获取静态资源文件的绝对路径
//        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);

        // 输入静态资源文件
        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()) {
            // 读取静态资源文件，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else{
            // 输出404
            output(HttpProtocolUtil.getHttpHeader404());
        }

    }

}
